package com.twu.excercise;

import java.util.ArrayList;
import java.util.List;

import static com.twu.excercise.Triangle.readNumber;

public class PrimeFactors {
    private static int SMALLEST_PRIME_NUMBER = 2;
    private static int MIN_DIVIDENT = 2;

    public static void main(String[] args) {
        int n=readNumber();
        List<Integer> numberList = generate(n);

        if(numberList.isEmpty())
            System.out.println("Number is prime");
        else{
            System.out.println(String.format("Prime factors of %s are",n));
            for(Integer num : numberList)
                System.out.println(num);
        }

    }

    private static List<Integer> generate(int n) {
        List<Integer> factorsOfN = new ArrayList<>();
        for(int i= SMALLEST_PRIME_NUMBER;i<n/2;i++) {
            if (isDivisible(n, i) && isPrime(i)) {
                factorsOfN.add(i);
            }
        }
        return factorsOfN;
    }

    private static boolean isDivisible(int divident, int divisor) {
        return divident % divisor == 0;
    }

    private static boolean isPrime(int n) {
        for(int i = MIN_DIVIDENT; i<=Math.sqrt(n);i++)
            if(isDivisible(n, i))
                return false;
        return true;
    }
}
